//获取所有页面的
$(function () {
    //加载散点图
    var dom = document.getElementById("analysisdata");
    var myChart = echarts.init(dom);
    var app = {};
    option = null;
    //获取当前当前页面的时间
    var currentDate = new Date();
    var currentYear = currentDate.getFullYear();
    var data = [];
    var bridgeId = 0;
    function getWarningDataSum(year)
    {
        //拼接时间
        year = year || 2019;
        var formatdate = year + '-01-01';
        var formatend = (+year + 1) + '-01-01';
        var date = +echarts.number.parseDate(formatdate);
        var end = +echarts.number.parseDate(formatend);
        var useddata = [];//用于对数组进行压缩,存放已经生产的月份
        var tmpsum = 0;
        var lastDay = "";
        //获取页面上桥的id
        var URL = document.location.toString();
        var arrUrl = URL.split("/");
        bridgeId = arrUrl[arrUrl.length-1] ;
        // 根据桥ID和时间去获取对应的数据
        $.ajax(
            {
                url:"/bridge/getallreport",
                type:"POST",
                dataType:"json",
                async:false,
                data:{BID:bridgeId,StartTime:formatdate,EndTime:formatend},
                success:function (result) {
                    $.each(result,function (i,item) {
                        //将数据添添加到数组中去
                        if(useddata.indexOf(item["reportDate"]) > -1)
                        {
                            //说明是日期已经存在
                            tmpsum += item["warningSum"];
                        }
                        else {
                            //说明是新的日期
                            //先处理上个日期
                            if (lastDay != "") {
                                data.push([
                                    echarts.format.formatTime('yyyy-MM-dd', lastDay),
                                    tmpsum
                                ]);
                            }
                            tmpsum = item["warningSum"] ;//初始化统计数据
                            lastDay = item["reportDate"];
                            useddata.push(item["reportDate"]);

                        }
                    });
                    //遍历完要把最后一次值push进去
                    data.push([
                        echarts.format.formatTime('yyyy-MM-dd', lastDay),
                        tmpsum
                    ]);
                },
                error: function (xhr, status, p3, p4) {
                    var err = "Error " + " " + status + " " + p3;
                    if (xhr.responseText && xhr.responseText[0] == "{")
                        err = JSON.parse(xhr.responseText).message;
                    console.log(err);
                }
            });
    }
    // function getVirtulData(year) {
    //     year = year || '2017';
    //     var date = +echarts.number.parseDate(year + '-01-01');
    //     var end = +echarts.number.parseDate((+year + 1) + '-01-01');
    //     var dayTime = 3600 * 24 * 1000;
    //     var data = [];
    //     for (var time = date; time < end; time += dayTime) {
    //         data.push([
    //             echarts.format.formatTime('yyyy-MM-dd', time),
    //             Math.floor(Math.random() * 10000)
    //         ]);
    //     }
    //     return data;
    // }

    getWarningDataSum(currentYear);
    //var data1 = getVirtulData(currentYear);
    //触发日历图点击事件
    myChart.on('click',function (params) {
        //console.log(params.data[1]);//需要拼接字符串跳转到指定页面
        var today = +echarts.number.parseDate(params.data[0]);
        var tomorrow = today + 24 * 3600 * 1000;
        var formattomorrow = echarts.format.formatTime('yyyy-MM-dd',tomorrow);
        window.location.href = "/bridge/getallreport/"+bridgeId+"/"+params.data[0]+"/"+formattomorrow;
    })
    option = {
        backgroundColor: '#404a59',

        title: {
            top: 30,
            text: '桥梁异常数据',
            subtext: '数据纯属虚构',
            left: 'center',
            textStyle: {
                color: '#fff'
            }
        },
        tooltip : {
            trigger: 'item'
        },
        legend: {
            top: '30',
            left: '100',
            data:['异常数据', 'Top 12'],
            textStyle: {
                color: '#ff0752'
            }
        },
        calendar: [{
            top: 100,
            left: 'center',
            range: [currentYear+'-01-01', currentYear+'-06-30'],
            splitLine: {
                show: true,
                lineStyle: {
                    color: '#000',
                    width: 4,
                    type: 'solid'
                }
            },
            yearLabel: {
                formatter: '{start}  1st',
                textStyle: {
                    color: '#fff'
                }
            },
            itemStyle: {
                normal: {
                    color: '#323c48',
                    borderWidth: 1,
                    borderColor: '#111'
                }
            }
        }, {
            top: 340,
            left: 'center',
            range: [currentYear+'-07-01', currentYear+'-12-31'],
            splitLine: {
                show: true,
                lineStyle: {
                    color: '#000',
                    width: 4,
                    type: 'solid'
                }
            },
            yearLabel: {
                formatter: '{start}  2nd',
                textStyle: {
                    color: '#fff'
                }
            },
            itemStyle: {
                normal: {
                    color: '#323c48',
                    borderWidth: 1,
                    borderColor: '#111'
                }
            }
        }],
        series : [
            {
                name: '异常数据',
                type: 'scatter',
                coordinateSystem: 'calendar',
                data: data,
                symbolSize: function (val) {
                    return val[1]/10;
                },
                itemStyle: {
                    normal: {
                        color: '#dd691e'
                    }
                }
            },
            {
                name: '异常数据',
                type: 'scatter',
                coordinateSystem: 'calendar',
                calendarIndex: 1,
                data: data,
                symbolSize: function (val) {
                    return val[1]/10;
                },
                itemStyle: {
                    normal: {
                        color: '#dd691e'
                    }
                }
            },
            {
                name: 'Top 12',
                type: 'effectScatter',
                coordinateSystem: 'calendar',
                calendarIndex: 1,
                data: data.sort(function (a, b) {
                    return b[1] - a[1];
                }).slice(0, 12),
                symbolSize: function (val) {
                    return val[1]/10;
                },
                showEffectOn: 'render',
                rippleEffect: {
                    brushType: 'stroke'
                },
                hoverAnimation: true,
                itemStyle: {
                    normal: {
                        color: '#ff0000',
                        shadowBlur: 10,
                        shadowColor: '#333'
                    }
                },
                zlevel: 1
            },
            {
                name: 'Top 12',
                type: 'effectScatter',
                coordinateSystem: 'calendar',
                data: data.sort(function (a, b) {
                    return b[1] - a[1];
                }).slice(0, 12),
                symbolSize: function (val) {
                    return val[1]/10;
                },
                showEffectOn: 'render',
                rippleEffect: {
                    brushType: 'stroke'
                },
                hoverAnimation: true,
                itemStyle: {
                    normal: {
                        color: '#ff0000',
                        shadowBlur: 10,
                        shadowColor: '#333'
                    }
                },
                zlevel: 1
            }
        ]
    };
    ;
    if (option && typeof option === "object") {
        myChart.setOption(option, true);
    }
})
$(function () {
    //此处获取搜索
    //获取页面上桥的id
    var URL = document.location.toString();
    var arrUrl = URL.split("/");
    bridgeId = arrUrl[arrUrl.length-1] ;
    $("#analysis").click(function () {
        var starttime = $("#startime").val();
        var endtime = $("#endtime").val();
        //跳转到分析页面
        window.location.href="/bridge/getallreport/"+bridgeId+"/"+starttime+"/"+endtime;
    })
})